package elcs.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import elcs.board.model.ListVO;
import elcs.member.model.MemberVO;
import elcs.mentoring.exception.AlreadyMentoringException;
import elcs.mentoring.exception.NoMentorException;
import elcs.mentoring.exception.NotEnoughPointException;
import elcs.mentoring.model.MentoringService;
import elcs.mentoring.model.MentoringVO;
import elcs.mentoring.model.NoteVO;
import elcs.point.model.TradePointService;

@Controller
public class MentoringController {
	@Resource(name = "mentoringServiceImpl")
	private MentoringService service;
	@Resource(name = "tradePointServiceImpl")
	private TradePointService tradePointservice;
	
	@RequestMapping("mentoring/mentoring.do")
	public String mentoring() {
		return "mentoring.mentoring";
	}

	@RequestMapping("mentoring/status.do")
	public ModelAndView statusView(HttpSession session) throws Exception {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		Map qMap = new HashMap();
		qMap.put("id", mvo.getId());
		qMap.put("page", 1);
		qMap.put("status", "all");
		return new ModelAndView("status.mentoring", "statusMap",
				service.statusView(qMap));
	}

	@RequestMapping("mentoring/statusAjax.do")
	@ResponseBody
	public ListVO statusViewAjax(HttpSession session, int page, String status)
			throws Exception {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		Map qMap = new HashMap();
		qMap.put("id", mvo.getId());
		qMap.put("page", page);
		qMap.put("status", status);
		return service.questionView(qMap);
	}

	/**
	 * Mentoring > Question > 멘토에게 질문하기 가능
	 * 
	 * @SSA
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/mentoring/question.do")
	public ModelAndView questionView(HttpSession session) throws Exception {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		List llist = service.getMyMentorLevel(mvo.getLevel());

		return new ModelAndView("question.mentoring", "llist", llist);
	}

	/**
	 * 선택한 레벨의 해당하는 임의의 멘토에게 질문을 전송한다. 레벨 별 해당되는 포인트가 감소된다. BY SSA
	 * 
	 * @throws Exception
	 * 
	 */
	@Transactional
	@RequestMapping("/mentoring/sendMentorQuestion.do")
	public ModelAndView sendMentorQuestion(NoteVO nvo, HttpSession session)
			throws Exception {
		ModelAndView mv=new ModelAndView();
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		nvo.setSender(mvo);
		mv.setViewName("error.mentoring");
		try{
			int point=service.sendMentorQuestion(nvo);
			tradePointservice.minusPoint(session, "쪽지 보내기", point);
			mv.setViewName("redirect:status.do");
		}catch(NotEnoughPointException e){
			mv.addObject("message",e.getMessage());
		}catch(NoMentorException e){
			mv.addObject("message",e.getMessage());
		}
		return mv;
	}

	@RequestMapping("/mentoring/rejectQuestion.do")
	public String rejectQuestionAjax(int no, int page, String status)
			throws Exception {
		service.rejectMentorQuestion(no);
		return "redirect:statusAjax.do?page=" + page + "&status=" + status;

	}

	@RequestMapping("/mentoring/viewContent.do")
	@ResponseBody
	public NoteVO viewContent(int no, HttpSession session) throws Exception {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		return service.getContent(no, mvo.getId());

	}

	@RequestMapping("/mentoring/senderDeleteNote.do")
	public String senderDeleteNoteAjax(int no, String uid) throws Exception {
		service.senderDeleteNote(no);
		return "redirect:getNoteListAjax.do?uid=" + uid;
	}

	@RequestMapping("/mentoring/receiverDeleteNote.do")
	public String receiverDeleteNoteAjax(int no, String uid) throws Exception {
		service.receiverDeleteNote(no);
		return "redirect:getNoteListAjax.do?uid=" + uid;
	}

	/**
	 * 답장 팝업창을 보여준다. by ssa
	 * 
	 * @throws Exception
	 * 
	 */
	@RequestMapping("/mentoring/replyView.do")
	public ModelAndView replyView(int no) throws Exception {
		NoteVO nvo = service.viewNoteContent(no);
		return new ModelAndView(
				"/WEB-INF/view/mentoring/mentoring_reply_popup", "nvo", nvo);
	}

	@RequestMapping("/mentoring/replyMentorQuestion.do")
	public ModelAndView replyMentorQuestion(NoteVO nvo, HttpSession session)
			throws Exception {
		service.replyMentorQuestion(nvo);
		return new ModelAndView("redirect:status.do");
	}

	@RequestMapping("/mentoring/deleteQuestion.do")
	public String deleteQuestionAjax(NoteVO nvo, HttpSession session, int page,
			String status) throws Exception {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		service.deleteNote(nvo, mvo.getId());
		return "redirect:statusAjax.do?page=" + page + "&status=" + status;
	}

	/**
	 * 답변을 받은 후 멘토에게 멘토신청을 한다. ssa
	 * 
	 * @param no
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/mentoring/sendMentorRequest.do")
	@ResponseBody
	public Map sendMentorRequest(int no, HttpSession session) throws Exception {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		Map map=new HashMap();
		try{
			List list=service.sendMentorRequest(no, mvo.getId());
			map.put("list", list);
		}catch(AlreadyMentoringException e){
			map.put("ame", e.getMessage());
		}
		return map;

	}

	/**
	 * 멘티가 멘토신청을 하면 수락 또는 거절을 할 수 있다.
	 * 
	 * @param mentoVO
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/mentoring/replyReceivedRequest.do")
	@ResponseBody
	public List replyReceivedRequestAjax(MentoringVO mentoVO) throws Exception {
		return service.replyReceivedRequest(mentoVO);

	}

	@RequestMapping("/mentoring/cancelRequest.do")
	@ResponseBody
	public List cancelRequestAjax(MentoringVO mentoVO) throws Exception {
		return service.cancelRequest(mentoVO);

	}

	/**
	 * 나의 멘토와 멘티 리스트를 보여준다.
	 * 
	 * @throws Exception
	 * 
	 */
	@RequestMapping("/mentoring/myMentoring.do")
	public ModelAndView myMentoringView(HttpSession session) throws Exception {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		return new ModelAndView("mymentoring.mentoring", "mentoringMap",
				service.myMentoringView(mvo.getId()));
	}

	@RequestMapping("/mentoring/getNoteList.do")
	public ModelAndView getNoteList(String uid, HttpSession session)
			throws Exception {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		Map map = new HashMap();
		map.put("uid", uid);
		map.put("myid", mvo.getId());
		List nlist = service.getNoteList(map);
		map.put("nlist", new JSONArray(nlist));
		return new ModelAndView("note.mentoring", "map", map);
	}

	@RequestMapping("/mentoring/getNoteListAjax.do")
	@ResponseBody
	public List getNoteListAjax(String uid, HttpSession session)
			throws Exception {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		Map map = new HashMap();
		map.put("uid", uid);
		map.put("myid", mvo.getId());
		return service.getNoteList(map);
	}

	@RequestMapping("/mentoring/sendNote.do")
	public String sendNote(NoteVO nvo, HttpSession session) throws Exception {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		nvo.setSender(mvo);
		service.sendNote(nvo);
		return "redirect:getNoteListAjax.do?uid=" + nvo.getReceiver().getId();
	}

	@RequestMapping("/mentoring/breakMentorRelationship.do")
	@ResponseBody
	public List breakMentorRelationship(MentoringVO mentoVO, HttpSession session)
			throws Exception {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		mentoVO.setMentee(mvo);
		return service.breakMentorRelationship(mentoVO);

	}

	@RequestMapping("/mentoring/breakMenteeRelationship.do")
	@ResponseBody
	public List breakMenteeRelationship(MentoringVO mentoVO, HttpSession session)
			throws Exception {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		mentoVO.setMentor(mvo);
		List list = service.breakMenteeRelationship(mentoVO);
		return list;

	}

	@RequestMapping("/mentoring/getPointForRequest.do")
	@ResponseBody
	public Map getPointForRequestAjax(int level, HttpSession session)
			throws Exception {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		Map map = new HashMap();
		int point = 0;
		try {
			point = service.getPointForRequest(level, mvo);
			map.put("point", point);
		} catch (NotEnoughPointException e) {
			map.put("nepe", e.getMessage());
		} catch (NoMentorException e) {
			map.put("nme", e.getMessage());
		}
		return map;
	}
	@RequestMapping("mentoring/getNewNote.do")
	@ResponseBody
	public Map getNewNote(String id) throws Exception{
		return service.CountNewNote(id);
	}
}
